package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    short a1 = 0;
    short a2 = 0;
    short a3 = 0;
    short a4 = 0;
    int temp = 0;

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.findById(id);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERROR;
        String msg = user != null ? "数据查询成功" : "数据查询失败，请重试";
        Result result = new Result(user, code, msg);
        return result;
    }

    @GetMapping
    public Result getAll() {
        List<User> userList = userService.findAll();
        Integer code = userList != null ? Code.GET_OK : Code.GET_ERROR;
        String msg = userList != null ? "数据查询成功" : "数据查询失败，请重试";
        return new Result(userList, code, msg);
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        boolean add = userService.add(user);
        String msg = add ? "数据添加成功" : "数据添加失败";
        return new Result(add, add ? Code.ADD_OK : Code.ADD_ERROR, msg);
    }

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id) {
        boolean delete = userService.delete(id);
        String msg = delete ? "数据删除成功" : "数据删除失败";
        return new Result(delete, delete ? Code.DELETE_OK : Code.DELETE_ERROR, msg);
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        if (user.getName().equals("张三")) {
            a1++;
            User user1 = new User("张三", a1);
            boolean update = userService.update(user1);
            String msg = update ? "数据修改成功" : "数据修改失败";
            return new Result(update, update ? Code.UPDATE_OK : Code.UPDATE_ERROR, msg);
        } else if (user.getName().equals("李四")) {
            a2++;
            User user2 = new User("李四", a2);
            boolean update = userService.update(user2);
            String msg = update ? "数据修改成功" : "数据修改失败";
            return new Result(update, update ? Code.UPDATE_OK : Code.UPDATE_ERROR, msg);
        } else if (user.getName().equals("王五")) {
            a3++;
            User user3 = new User("王五", a3);
            boolean update = userService.update(user3);
            String msg = update ? "数据修改成功" : "数据修改失败";
            return new Result(update, update ? Code.UPDATE_OK : Code.UPDATE_ERROR, msg);
        } else {
            a4++;
            User user4 = new User("赵六", a4);
            boolean update = userService.update(user4);
            String msg = update ? "数据修改成功" : "数据修改失败";
            return new Result(update, update ? Code.UPDATE_OK : Code.UPDATE_ERROR, msg);
        }
    }


    @PutMapping("/init")
    public void init(){
        if(temp==0){
                User user1 = new User("张三", (short) 0);
                User user2 = new User("李四", (short) 0);
                User user3 = new User("王五", (short) 0);
                User user4 = new User("赵六", (short) 0);
                userService.update(user1);
                userService.update(user2);
                userService.update(user3);
                userService.update(user4);
                temp++;
            }
    }
}

